package com.bwsoft.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.News;
import com.bwsoft.admin.domain.NewsCategory;
import com.bwsoft.api.action.NewsAction;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.service.NewsService;
import com.bwsoft.api.util.Constant;
@Service(NewsService.NAME)
public class NewsServiceImpl extends BaseServiceImpl implements NewsService{
	private static Logger LOG = Logger.getLogger(NewsServiceImpl.class);
	
	/**
	 * 获取资讯类别
	 */
	@Override
	public ResponsePojo getCategory() {
		ResponsePojo rp=new ResponsePojo();
		rp.setErrcode(0);
		rp.setErrmsg("请求成功");
		String hql="from NewsCategory as nc where nc.status=1 order by nc.order";
		LOG.info("HQL语句为"+hql);
		List<NewsCategory> newsCategories =newsCategoryDao.findByHql(hql, null);
		LOG.info("得到【"+newsCategories.size()+"】条数据");
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		for (NewsCategory newsCategory : newsCategories) {
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("id", newsCategory.getId());
			map.put("order", newsCategory.getOrder());
			map.put("name", newsCategory.getName());
			list.add(map);
		}
		rp.setData(list);
		return rp;
	}

	
	/**
	 * 按照类别进行分页查询资讯
	 */
	@Override
	public ResponsePojo getList(Integer category, Integer page) {
		
		ResponsePojo rp=new ResponsePojo();
		List<News> news=new ArrayList<News>();
		List<News> lunbo=new ArrayList<News>();
		String count="";
		String hql = "from News as n where n.category.id=? and n.status=1 and n.lunboFlag=? order by n.addTime desc";
		String sizeHql = "select count(*) " + hql;
		try{
			//查询列表集合大小
			LOG.info("开始查询该分类下所有集合的数量");
			count=newsDao.findByHql(sizeHql, category,0==1).get(0).toString();
			LOG.info("开始查询资讯集合");
			news=newsDao.findPage(hql,Constant.PAGE_COUNT , (page-1)*Constant.PAGE_COUNT, category,0==1);
			if (page == 1) {
				LOG.info("开始查询轮播图集合");
				lunbo = newsDao.findPage(hql, Constant.LUNBO_COUNT, 0,category, 1 == 1);
			}
		}catch (Exception e) {
			LOG.info("异常"+e.getStackTrace());
			rp.setErrcode(50000);
			rp.setErrmsg("服务器异常");
			rp.setData(e);
			return rp;
		}
		rp.setErrcode(0);
		rp.setErrmsg("请求成功");
		Map<String ,Object> returnMap=new HashMap();
		returnMap.put("category", category);
		returnMap.put("totalPage", Integer.parseInt(count)/Constant.PAGE_COUNT+1);
		returnMap.put("pageNow", page);
		if (page == 1) {
			returnMap.put("lunbo", newsToMap(lunbo));
		}
		returnMap.put("news", newsToMap(news));
		rp.setData(returnMap);
		return rp;
	}
	
	public List<Map<String,Object>> newsToMap(List<News> news){
		List<Map<String,Object>> newsList=new ArrayList<Map<String,Object>>();
		for (News ne : news) {
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("newsid",ne.getNewsId() );
			map.put("title", ne.getTitle());
			map.put("summary", ne.getSummary());
			map.put("imgURL", ne.isLunboFlag()?ne.getLunboImg():ne.getImgURL());
			map.put("linkURL", "/api/news_page.action?newsId="+ne.getNewsId());
			newsList.add(map);
		}
		return newsList;
	}


	@Override
	public News findNews(Integer newsId) {
		return newsDao.findById(newsId);
	}
	
	

}
